import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-03-02
 * Time: 0:30
 */
public class test2 {
    /**
     * LeetCode 433  最小基因变化
     */

    public int minMutation(String startGene, String endGene, String[] bank) {
        // 标记已经搜索过的字符串
        Set<String> vis = new HashSet<>();
        Set<String> hash = new HashSet<>();
        for(String s : bank){
            hash.add(s);
        }
        // 所有基因
        char[] change = {'A','C','G','T'};
        if(startGene.equals(endGene)){
            return 0;
        }
        if(!hash.contains(endGene)){
            return -1;
        }
        Queue<String> q = new LinkedList<>();
        q.add(startGene);
        vis.add(startGene);
        int step = 0;
        while(!q.isEmpty()){
            step++;
            int sz = q.size();
            while(sz--!=0){
                String t = q.poll();
                for(int i=0;i<8;i++){
                    char[] tmp = t.toCharArray();
                    for(int j=0;j<4;j++){
                        tmp[i] = change[j];
                        String next = new String(tmp);
                        if(hash.contains(next) && !vis.contains(next)){
                            if(next.equals(endGene)){
                                return step;
                            }
                            q.add(next);
                            vis.add(next);
                        }
                    }
                }
            }
        }
        return -1;
    }
}
